/**
 * 
 */
package sailing.ui.base;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import org.jdesktop.application.SingleFrameApplication;

import sailing.filters.ui.FilterTablePanel;
import sailing.listener.UI;
import sailing.ui.AbstractJPanel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class CenterPanel extends AbstractJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private FilterTablePanel filterTablePanel;

	/**
	 * @param application
	 */
	public CenterPanel(SingleFrameApplication application) {
		super(application);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.AbstractJPanel#initialPanel()
	 */
	@Override
	protected void initialPanel() {
		JPanel root = new JPanel();
		// JPanel root = new FormDebugPanel(true, true);
		FormLayout layout = new FormLayout("0dlu,f:250dlu:g,2dlu,f:73dlu,1dlu", "1dlu,f:18dlu:g,1dlu");
		root.setLayout(layout);

		CellConstraints cc = new CellConstraints();

		root.add(this.getFilterTablePanel(), cc.xy(2, 2));
		root.add(this.getSidebarPanel(), cc.xy(4, 2));

		this.setLayout(new BorderLayout());
		this.add(root, BorderLayout.CENTER);
	}

	private Component getFilterTablePanel() {
		if (filterTablePanel == null) {
			filterTablePanel = new FilterTablePanel(this.getApplication());
		}
		return filterTablePanel;
	}

	private Component getSidebarPanel() {
		// JPanel sidebar = new FormDebugPanel(true, true);
		JPanel sidebar = new JPanel();
		sidebar.setBorder(BorderFactory.createTitledBorder(""));
		FormLayout layout = new FormLayout("1dlu,f:pref,1dlu", "2dlu,f:18dlu,3dlu,f:18dlu,f:18dlu:g");
		sidebar.setLayout(layout);

		CellConstraints cc = new CellConstraints();

		JCheckBox showHideFilterPanel = new JCheckBox("显示隐藏筛选面板");
		showHideFilterPanel.setFont(UI.font12);
		sidebar.add(showHideFilterPanel, cc.xy(2, 2));

		showHideFilterPanel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() instanceof JCheckBox) {
					JCheckBox jcb = (JCheckBox) e.getSource();
					filterTablePanel.relayout(jcb.isSelected());
					CenterPanel.this.doLayout();
				}
			}
		});

		if (filterTablePanel != null) {
			boolean show = filterTablePanel.getShowed();
			showHideFilterPanel.setSelected(show);
		}

		JCheckBox showhideEditBtn = new JCheckBox();
		showhideEditBtn.setText("显示隐藏编辑按钮");
		showhideEditBtn.setFont(UI.font12);
		sidebar.add(showhideEditBtn, cc.xy(2, 4));

		return sidebar;
	}
}
